package cn.com.bluemoon.daps.sync.service.impl;

import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.sync.entity.DapSyncTaskOperRecord;
import cn.com.bluemoon.daps.sync.mapper.DapSyncTaskOperRecordMapper;
import cn.com.bluemoon.daps.sync.service.DapSyncTaskOperRecordService;
import cn.com.bluemoon.daps.sync.vo.model2prod.SyncOperRecordOut;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 同步功能操作记录表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-06-17
 */
@Slf4j
@Service
public class DapSyncTaskOperRecordServiceImpl extends ServiceImpl<DapSyncTaskOperRecordMapper, DapSyncTaskOperRecord>
        implements DapSyncTaskOperRecordService {

    /**
     * 模型发布同步任务执行操作记录
     *
     * @param modelId  模型
     * @param taskId   发布同步任务id {@link cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask#getId()}
     * @param contents 记录内容
     * @return 记录id
     */
    @Override
    public List<String> marks(String modelId, String taskId, List<String> contents) {
        BmAssetUtils.notNull(contents, "记录内容不可为空");
        return contents.stream().map(c -> mark(modelId, taskId, c, LocalDateTime.now())).collect(Collectors.toList());
    }

    /**
     * 模型发布同步任务执行操作记录
     *
     * @param modelId 模型
     * @param taskId  发布同步任务id {@link cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask#getId()}
     * @param content 记录内容
     * @return 记录id
     */
    @Override
    public String mark(String modelId, String taskId, String content) {
        List<String> contents = Collections.singletonList(content);
        return contents.stream().map(c -> mark(modelId, taskId, c, LocalDateTime.now())).findFirst().orElse(null);
    }


    /**
     * 模型发布同步任务执行操作记录
     *
     * @param modelId  模型
     * @param taskId   发布同步任务id {@link cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask#getId()}
     * @param content  记录内容
     * @param markTime 指定记录时间
     * @return 记录id
     */
    @Override
    public String mark(String modelId, String taskId, String content, LocalDateTime markTime) {
        BmAssetUtils.notNull(content, "记录内容不堪为空");
        DapSyncTaskOperRecord operRecord = new DapSyncTaskOperRecord();
        operRecord.setModelId(modelId);
        operRecord.setTaskId(taskId);
        operRecord.setContent(content);
        operRecord.setRecordTime(markTime);
        boolean save = super.save(operRecord);
        log.info("记录同步日志情况{}，status:{}", content, save);
        return operRecord.getId();
    }

    /**
     * 日志
     *
     * @param modelId 模型
     * @param taskId  任务id
     * @return list
     */
    @Override
    public List<SyncOperRecordOut> findOpersByTaskId(String modelId, String taskId) {
        LambdaQueryWrapper<DapSyncTaskOperRecord> eq = bmLambdaQuery().eq(DapSyncTaskOperRecord::getModelId, modelId)
                .eq(DapSyncTaskOperRecord::getTaskId, taskId)
                .orderByDesc(DapSyncTaskOperRecord::getCreateTime)
                .orderByDesc(DapSyncTaskOperRecord::getId);
        List<DapSyncTaskOperRecord> operRecords = list(eq);
        return operRecords.stream()
                .map(d -> SyncOperRecordOut.builder()
                        .operId(d.getId()).content(d.getContent())
                        .recordTime(d.getRecordTime()).build())
                .collect(Collectors.toList());
    }
}
